package com.dmz.workflow.util;

import com.echronos.commons.utils.RequestUserUtils;
import com.dmz.workflow.constant.FormComponentConstants;
import com.dmz.workflow.constant.LanguageExpressionConstants;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;

import java.util.*;

@Slf4j
public class ConditionalDataProcessing {

    public static Map<String, Object> getStringObjectMap(Map<String, Object> requestParameter) {
        Map<String, Object> expressionParameter = new HashMap<>();
        expressionParameter.put(FormComponentConstants.STARTER_COMPONENT, String.valueOf(RequestUserUtils.getUser().getMemberId()));
        if (ObjectUtils.isNotEmpty(requestParameter)) {
            for (Map.Entry<String, Object> entry : requestParameter.entrySet()) {
                String k = entry.getKey();
                Object v = entry.getValue();
                if (k.contains(FormComponentConstants.MULTIPLE_SELECTION_COMPONENT)) {
                    if (v instanceof List) {
                        try {
                            List stringList = (List) v;
                            String join = StringUtils.join(stringList, LanguageExpressionConstants.LANGUAGE_COMMA);
                            expressionParameter.put(k, join);
                        } catch (Exception exception) {
                            expressionParameter.put(k, v);
                        }
                    }
                } else if (k.contains(FormComponentConstants.NUMBER_COMPONENT) || k.contains(FormComponentConstants.AMOUNT_COMPONENT)
                        || k.contains(FormComponentConstants.DATE_RANGE_COMPONENT) || k.contains(FormComponentConstants.DEPARTMENT_COMPONENT)) {
                    Double value = null;
                    if (ObjectUtils.isNotEmpty(v)) {
                        if (v instanceof String) {
                            String intStr = String.valueOf(v);
                            value = Double.valueOf(intStr);
                        } else {
                            value = (Double) v;
                        }
                    }
                    expressionParameter.put(k, value);
                } else if (k.contains(FormComponentConstants.SINGLE_SELECTION_COMPONENT)) {
                    expressionParameter.put(k, v);
                }
            }
        }
        return expressionParameter;
    }

}
